<?php

/**
 * ECSHOP 程序说明
 * ===========================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ==========================================================
 * $Author: liubo $
 * $Id: magazine_list.php 17217 2011-01-19 06:29:08Z liubo $
 */

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
admin_priv('sms_send');
if ($_REQUEST['act'] == 'list')
{
    $smarty->assign('ur_here', $_LANG['03_sms_send']);
    $smarty->assign('action_link', array('text' => '添加短信模板', 'href' => 'sms_new.php?act=add'));
    $smarty->assign('full_page',  1);

    $list = get_sms_templates();

    $smarty->assign('list',   $list['list']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);

//     $special_ranks = get_rank_list();
//     $send_rank[SEND_LIST . '_0'] = $_LANG['email_user'];
//     $send_rank[SEND_USER . '_0'] = $_LANG['user_list'];
//     foreach($special_ranks as $rank_key => $rank_value)
//     {
//         $send_rank[SEND_RANK . '_' . $rank_key] = $rank_value;
//     }
//     $smarty->assign('send_rank',   $send_rank);

    assign_query_info();
    $smarty->display('sms_template_list.htm');
}
elseif ($_REQUEST['act'] == 'query')
{
    $list = get_sms_templates();

    $smarty->assign('list',   $list['list']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']);

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('sms_template_list.htm'), '', array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}
elseif ($_REQUEST['act'] == 'add')
{
    if (empty($_POST['step']))
    {
//         include_once(ROOT_PATH.'includes/fckeditor/fckeditor.php'); // 包含 html editor 类文件
        $smarty->assign('action_link', array('text' => '返回列表', 'href' => 'sms_new.php?act=list'));
        $smarty->assign(array('ur_here'=>$_LANG['03_sms_send'],'act'=>'add'));
//         create_html_editor('magazine_content');
        assign_query_info();
        $smarty->display('sms_template_add.htm');
    }
    elseif ($_POST['step'] == 2)
    {
        $tpl_id = intval($_POST['tpl_id']);
        $tpl_title = trim($_POST['tpl_title']);
        $tpl_code = trim($_POST['tpl_code']);
        $time = gmtime();
        $sql = "INSERT INTO " . $ecs->table('sms_templates') . " (tpl_id, tpl_title, last_modify, tpl_code) VALUES('$tpl_id','$tpl_title', '$time', '$tpl_code')";
        $db->query($sql);
        $links[] = array('text' => '返回列表', 'href' => 'sms_new.php?act=list');
        $links[] = array('text' => '继续添加', 'href' => 'sms_new.php?act=add');
        sys_msg('添加成功', 0, $links);
    }
}
elseif ($_REQUEST['act'] == 'edit')
{
//     include_once(ROOT_PATH.'includes/fckeditor/fckeditor.php'); // 包含 html editor 类文件
    $id = intval($_REQUEST['id']);
    if (empty($_POST['step']))
    {
        $rt = $db->getRow("SELECT * FROM "  . $ecs->table('sms_templates') . " WHERE id = '$id'");
//         print_r($rt);
		
        $smarty->assign(array('id'=>$id,'act'=>'edit','tpl'=>$rt));
        $smarty->assign(array('ur_here'=>'编辑短信模板','act'=>'edit'));
        $smarty->assign('action_link', array('text' => '返回列表', 'href' => 'sms_new.php?act=list'));
//         create_html_editor('magazine_content', $rt['template_content']);
        assign_query_info();
        $smarty->display('sms_template_add.htm');
    }
    elseif ($_POST['step'] == 2)
    {
    	$id = intval($_POST['id']);
        $tpl_id = intval($_POST['tpl_id']);
        $tpl_title = trim($_POST['tpl_title']);
        $tpl_code = trim($_POST['tpl_code']);
        $time = gmtime();
        $db->query("UPDATE " . $ecs->table('sms_templates') . " SET tpl_id = '$tpl_id', tpl_title = '$tpl_title', tpl_code = '$tpl_code', last_modify = '$time' WHERE id = '$id'");
        $links[] = array('text' => '返回列表', 'href' => 'sms_new.php?act=list');
        sys_msg('编辑成功', 0, $links);
    }
}
elseif ($_REQUEST['act'] == 'del')
{
    $id = intval($_REQUEST['id']);
    $db->query("DELETE  FROM "  . $ecs->table('sms_templates') . " WHERE id = '$id' LIMIT 1");
    $links[] = array('text' => '返回列表', 'href' => 'sms_new.php?act=list');
    sys_msg('删除成功', 0, $links);
}
elseif ($_REQUEST['act'] == 'addtolist')
{
    $id         = intval($_REQUEST['id']);
    $start      = empty($_GET['start']) ? 0 : intval($_GET['start']);
   
	if ($db->getOne('SELECT id FROM '.$ecs->table('sms_templates').' WHERE id='.$id.' AND tpl_code="normal"')<=0){
		sys_msg ( '选择短信模板错误', 1);
	}
   $count_sql = 'SELECT COUNT(*) FROM ' . $ecs->table('users');
   $mobile_sql = 'SELECT mobile_phone,user_id FROM ' . $ecs->table('users') . " LIMIT $start,100";

   $count = $db->getOne($count_sql);
	if ($count > $start) {
		$query = $db->query ( $mobile_sql );
		$add = '';
		
		$i = 0;
		while ( $rt = $db->fetch_array ( $query ) ) {
			$time = gmtime ();
			$add .= $add ? ",('$rt[mobile_phone]','$id','$rt[user_id]')" : "('$rt[mobile_phone]','$id','$rt[user_id]')";
			$i ++;
		}
		if ($add) {
			$sql = "INSERT INTO " . $ecs->table ( 'sms_sendlist' ) . " (phone,template_id,user_id) VALUES " . $add;
			$db->query ( $sql );
		}
		if ($i == 100) {
			$start = $start + 100;
		} else {
			$start = $start + $i;
		}
		$links [] = array (
				'text' => '已经插入'.$start.' 条记录,请稍后~',
				'href' => "sms_new.php?act=addtolist&id=$id&start=$start" 
		);
		sys_msg ( '正在生成请稍后', 0, $links );
	} else {
		$db->query ( "UPDATE " . $ecs->table ( 'sms_templates' ) . " SET last_send = " . gmtime () . " WHERE id = '$id'" );
		$links [] = array (
				'text' => '短信模板列表',
				'href' => 'sms_new.php?act=list' 
		);
		sys_msg ( '编辑成功', 0, $links );
	}
        
    
}


function get_sms_templates()
{
    $result = get_filter();

    if ($result === false)
    {
        $filter['sort_by']      = empty($_REQUEST['sort_by']) ? 'tpl_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order']   = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

        $sql = "SELECT count(*) FROM " .$GLOBALS['ecs']->table('sms_templates');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        /* 分页大小 */
        $filter = page_and_size($filter);

        /* 查询 */
        $sql = "SELECT * ".
               " FROM ".$GLOBALS['ecs']->table('sms_templates') .
               " ORDER by " . $filter['sort_by'] . ' ' . $filter['sort_order'] .
               " LIMIT " . $filter['start'] . ',' . $filter['page_size'];

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $list = $GLOBALS['db']->getAll($sql);

    foreach($list as $k => $v)
    {
        $list[$k]['last_modify'] = local_date('Y-m-d', $v['last_modify']);
        $list[$k]['last_send'] = local_date('Y-m-d', $v['last_send']);
    }

    $arr = array('list' => $list, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);

    return $arr;
}

?>